package com.yxb.cms.controller.view.property;


import com.yxb.cms.architect.constant.BussinessCode;
import com.yxb.cms.architect.utils.BussinessMsgUtil;
import com.yxb.cms.controller.view.BaseController;
import com.yxb.cms.dao.DbServerManagerMapper;
import com.yxb.cms.domain.dto.BussinessMsg;
import com.yxb.cms.domain.vo.DbServerManager;
import com.yxb.cms.service.ServerManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 资产管理-云资源管理
 *
 * @author YXB
 */
@Controller
@RequestMapping(value = "/property")
public class ServerManagerController extends BaseController {



    @Autowired
    private ServerManagerService serverManagerService;

    @Autowired
    private DbServerManagerMapper  serverManagerMapper;

    /**
     * 跳转到云资源管理页面
     *
     */
    @RequestMapping("/server_manager_page.action")
    public String toServerManagerPage() {
        return "property/server_manager_page";
    }


    /**
     * 跳转到云资源新增页面
     *
     * @return
     */
    @RequestMapping("/server_manager_add.action")
    public String serverManagerAdd(Model model) {
        model.addAttribute("pageFlag", "addPage");
        return "property/server_manager_edit";
    }

    /**
     * 跳转到云资源修改页面
     *
     * @return
     */
    @RequestMapping("/server_manager_update.action")
    public String serverManagerUpdate(Model model,String serverId) {
        model.addAttribute("pageFlag", "updatePage");
        DbServerManager server = serverManagerMapper.selectByPrimaryKey(serverId);
        model.addAttribute("server",server);
        return "property/server_manager_edit";
    }




    /**
     * 保存云资源信息
     * @return
     */
    @RequestMapping(value="/ajax_save_server_manager.action")
    @ResponseBody
    public BussinessMsg ajaxSaveServerManage(DbServerManager serverManager){
        try {
            return serverManagerService.saveOrUpdateServerManager(serverManager,this.getCurrentUserId());
        } catch (Exception e) {
            log.error("保存云资源信息内部错误{}", e.getMessage(), e);
            return BussinessMsgUtil.returnCodeMessage(BussinessCode.SERVER_SAVE_ERROR);
        }

    }






    /**
     * 云资源信息分页展示
     *
     * @return
     */
    @RequestMapping("/ajax_server_list.action")
    @ResponseBody
    public Map<String, Object> ajaxServerList(DbServerManager serverManager) {
        return serverManagerService.selectServerListByPage(serverManager);
    }

}
